package cn.com.self.apis.portal.service.impl;

import cn.com.self.apis.portal.entity.SysMenuEntity;
import cn.com.self.apis.portal.entity.SysRoleMenuEntity;
import cn.com.self.apis.portal.mapper.SysMenuMapper;
import cn.com.self.apis.portal.mapper.SysRoleMenuMapper;
import cn.com.self.apis.portal.service.intf.IMenuService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.portal.vo.TreeNodeModel;
import cn.com.self.apis.portal.vo.TreeNodesModel;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description
 */
@Service
public class SysMenuService extends BaseServiceImpl<SysMenuEntity, String> implements IMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public ICustomPage<SysMenuEntity> selectPage(Map<String, Object> queryParams, Page pageable) {
        QueryWrapper<SysRoleMenuEntity> queryWrapper =  new QueryWrapper<>();
        QueryWrapper<SysMenuEntity> MenuQuserWrapper =  new QueryWrapper<>();

        queryParams.keySet().forEach(key -> {
            if(("id").equals(key)){
                queryWrapper.eq("id", queryParams.get("id"));
            }else if("role_id".equals(key)) {
                queryWrapper.eq("role_id",queryParams.get("role_id"));
            }else if("Menu_id".equals(key)) {
                queryWrapper.eq("Menu_id",queryParams.get("Menu_id"));
            }else if("name".equals(key)) {
                MenuQuserWrapper.like("name",queryParams.get("name"));
            }
        });

        //获取角色已授权菜单的关联关系数据
        List<SysRoleMenuEntity> list = sysRoleMenuMapper.selectList(queryWrapper);
        List<Long> privileIds = getMenuIds(list);
        if (privileIds!=null&&privileIds.size()>0){
            MenuQuserWrapper.in("id",privileIds);
        }else{
        MenuQuserWrapper.in("id","");
        }
        if(null != queryParams.get("tree_id")){
            MenuQuserWrapper.in("parent", queryParams.get("tree_id"));
        }
        //获取符合条件的已授权菜单数据
        ICustomPage<SysMenuEntity> pageList =  (ICustomPage<SysMenuEntity>)sysMenuMapper.selectPage(pageable,MenuQuserWrapper);
       // List<SysMenuEntity> aalist=sysMenuMapper.loadTwoSysMenu(queryParams);
        return pageList;
    }
    public List<SysMenuEntity> loadOneTree(){
        Map<String,Object> map=new HashedMap();
        List<SysMenuEntity> SysMenuTree=sysMenuMapper.loadOneSysMenu(map);
        return SysMenuTree;
    }


    @Override
    public ICustomPage<SysMenuEntity> ntRolePriviPage(Map<String, Object> queryParams, Page pageable) {
        QueryWrapper<SysRoleMenuEntity> queryWrapper =  new QueryWrapper<>();
        QueryWrapper<SysMenuEntity> MenuQuserWrapper =  new QueryWrapper<>();

        //如果当前所选角色是第一级角色
        if (queryParams.get("pid").equals("-1")){
            queryParams.keySet().forEach(key -> {
                if(("id").equals(key)){
                    queryWrapper.eq("id", queryParams.get("id"));
                }else if("role_id".equals(key)) {
                    queryWrapper.eq("role_id",queryParams.get("role_id"));
                }else if("name".equals(key)) {
                    MenuQuserWrapper.like("name",queryParams.get("name"));
                }
            });

            //获取角色已授权菜单的关联关系数据
            List<SysRoleMenuEntity> list = sysRoleMenuMapper.selectList(queryWrapper);
            List<Long> privileIds = getMenuIds(list);
            if (privileIds!=null&&privileIds.size()>0){
                MenuQuserWrapper.notIn("id",privileIds);
            }else {
                MenuQuserWrapper.notIn("id","");

            }
            MenuQuserWrapper.eq("type","MENU");

            //获取符合条件的已授权菜单数据
            ICustomPage<SysMenuEntity> pageList =  (ICustomPage<SysMenuEntity>)sysMenuMapper.selectPage(pageable,MenuQuserWrapper);
            return pageList;

        }else {
            queryParams.keySet().forEach(key -> {
                if(("id").equals(key)){
                    queryWrapper.eq("id", queryParams.get("id"));
                }else if("pid".equals(key)) {
                    queryWrapper.eq("role_id",queryParams.get("pid"));
                }else if("name".equals(key)) {
                    MenuQuserWrapper.like("name",queryParams.get("name"));
                }
            });

            List<SysRoleMenuEntity> list = sysRoleMenuMapper.selectList(queryWrapper);
            List<Long> privileIds = getMenuIds(list);

            if (privileIds!=null&&privileIds.size()>0){
                MenuQuserWrapper.in("id",privileIds);
            }else {
                MenuQuserWrapper.in("id","");

            }
            MenuQuserWrapper.eq("type","MENU");

            //获取符合条件的已授权菜单数据
            ICustomPage<SysMenuEntity> pageList =  (ICustomPage<SysMenuEntity>)sysMenuMapper.selectPage(pageable,MenuQuserWrapper);
            return pageList;

        }

    }

    @Override
    public void grantPrivile(List<SysRoleMenuEntity> list) {
        for (SysRoleMenuEntity entity:list){
            QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",entity.getRoleId());
            queryWrapper.eq("Menu_id",entity.getMenuId());
            //判断同角色、菜单是否已经被授权
            SysRoleMenuEntity info = sysRoleMenuMapper.selectOne(queryWrapper);
            if (info==null){
                entity.setCreateTime(new Date());
                entity.setUpdateTime(new Date());
                sysRoleMenuMapper.insert(entity);

            }
        }
    }

    @Override
    public void deletes(List<SysRoleMenuEntity> list) {
        for (SysRoleMenuEntity info :list){
            QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",info.getRoleId());
            queryWrapper.eq("Menu_id",info.getMenuId());
            sysRoleMenuMapper.delete(queryWrapper);
        }
    }


    private  List<Long> getMenuIds( List<SysRoleMenuEntity> list ){
        List<Long> MenuList = new ArrayList<>();
        for (SysRoleMenuEntity info:list){
            MenuList.add(info.getMenuId());
        }
        return  MenuList;
    }

    @Override
    public int roleAdd(SysRoleMenuEntity sysRoleMenuEntity) {

        return sysRoleMenuMapper.insert(sysRoleMenuEntity);
    }

    @Override
    public List<Map<String,Object>>  loadOneSysMenu(String id) {
       List<Map<String,Object>> rtList=new ArrayList<>();
        Map<String,Object> parameterMap=new HashMap<>();
        Map<String,Object> idMap=new HashMap<>();
        idMap.put("id",id);
        List<SysMenuEntity> SysMenuIdList=sysMenuMapper.loadOneSysMenu(idMap);
        for(SysMenuEntity sysMenu : SysMenuIdList) {
            Map<String,Object> rtMap2=new HashMap<>();
            parameterMap.put("id",id);
            parameterMap.put("parent",sysMenu.getId().toString());
            List<SysMenuEntity> list=sysMenuMapper.loadTwoSysMenu(parameterMap);
            rtMap2.put("code",sysMenu.getCode());
            rtMap2.put("depth",sysMenu.getDepth());
            rtMap2.put("icon",sysMenu.getIcon());
            rtMap2.put("link",sysMenu.getLink());
            rtMap2.put("name",sysMenu.getName());
            rtMap2.put("position",sysMenu.getPosition());
            rtMap2.put("type",sysMenu.getType());
            rtMap2.put("Menu",sysMenu.getStatus());
            rtMap2.put("children",list);
            rtList.add(rtMap2);
        }
        return rtList;
    }

    /**构造角色树
     * @Description //TODO
     * @return
     **/
    @Override
    public TreeNodesModel createMenuTree(Map<String, Object> queryMap) {
        QueryWrapper<SysMenuEntity> queryWrapper = new QueryWrapper<>();
        TreeNodesModel result = new TreeNodesModel();
        //获取出所有的角色
        List<SysMenuEntity> list  = new ArrayList<>();
        list = sysMenuMapper.selectList(queryWrapper);
        int total = list.size();
        //对角色进行树形构造
        List<TreeNodeModel> roleList = new ArrayList<TreeNodeModel>();
        for (int i =0;i<total;i++){
            SysMenuEntity info = list.get(i);
            TreeNodeModel treeNode = new TreeNodeModel();
            treeNode.setCode(info.getCode());
            treeNode.setId(String.valueOf(info.getId()));
            treeNode.setName(info.getName());
            treeNode.setPId(info.getParent());
            roleList.add(treeNode);
        }

        result.setDatas(roleList);
        result.setTotal(total);
        return result;

    }

}
